// firstPackage/pages/msgModule/sysMsgList/sysMsgList.js

import {
  querySystemMsgList,
  batchReadSystemMsg,
  readSingleSystemMsg
} from "../../../../service/msg_service"
import {
  msgList,
  singleMsgRead,
} from "../../../../service/jb_common_service"

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    sysList: [

    ],
    page: 1,
    pageSize: 10,
    scrollRefreshStatus: true,
    isShowLoading: false,
    isBottomShow: false,
    refreshFlag: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.querySystemMsgList('refresh');
    // this.batchReadSystemMsg();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  onUnload() {
    //默认就全部已读

  },

  //跳转消息详情：
  pushToMsgDetail(e) {
    let index = e.currentTarget.dataset.index;
    let item = this.data.sysList[index];
    if (item.linkUrl || item.path) {
      if (item.linkUrl) {
        //跳转webView
        app.globalData.webUrl = item.linkUrl;
        wx.navigateTo({
          url: "/pages/webViewPage/webViewPage",
        })
      } else {
        item.isRead = 1
        let changeItem = 'sysList[' + index + ']';
        this.setData({
          [changeItem]: item,
        })
        this.singleMsgRead(item.id);
        //跳转对应页面:
        wx.navigateTo({
          url: item.path,
        })
      }
    }

  },
  singleMsgRead(id) {
    singleMsgRead(id).then(res => { })
  },


  // 下拉刷新
  handleRefresh() {
    // TODO
    this.setData({
      page: 1,
      isBottomShow: false,
      refreshFlag: true,
    })
    this.querySystemMsgList('refresh');
  },
  // 加载更多
  handleLoadmore() {
    console.log('加载更多');
    if (this.data.isBottomShow) {
      return;
    }
    this.setData({
      isShowLoading: true,
      page: this.data.page + 1
    })
    this.querySystemMsgList()
  },


  //获取系统消息列表:
  querySystemMsgList(type) {
    wx.showLoading();

    msgList(this.data.page, this.data.pageSize).then(res => {
      console.log("JJLog:系统消息列表:" + JSON.stringify(res));
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false,
      })
      if (res.code == 200) {
        let arr = (type == 'refresh' ? [] : this.data.sysList || []);
        arr = arr.concat(res.data.list || [])
        this.setData({
          sysList: arr,
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: res.data.totalPage == this.data.page ? true : false,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      this.setData({
        refreshFlag: false,
      })

      let page = this.data.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        page: page
      })
      console.error("JJLog:querySystemMsgList", err);
    })
  },
  //系统消息全部已读
  batchReadSystemMsg() {
    batchReadSystemMsg().then(res => {

    }).catch(err => {
      console.error("JJLog:batchReadSystemMsg", err);
    })
  },

  //到底继续加载
  scrolltolower() {
    if (this.data.isBottomShow) {
      //已经到底了就不加载了
      return;
    }
    let tempPage = this.data.page + 1;
    this.setData({
      page: tempPage,
      isShowLoading: true,
    })
    this.querySystemMsgList();
  },

  //下拉刷新:
  refreshDownData() {

    console.log("JJLog:下拉刷新来了=====:");
    this.setData({
      scrollRefreshStatus: true,
      page: 0,
      isBottomShow: false,
    })
    this.querySystemMsgList();
  },


})